<?php

//注册事件
Event::listen('register_login_event', function ($user, $request) {
	try {
		// 看看请求中是否有传openid，如果有的话就和现在的账号绑定
		if (isset($request['openid'])) {
			// 如果此用户没有绑定的openid而且此openid没有被别人使用，则可以绑定
			if (!$user->wx_openid) {
				$existingUser = null;
				try {
					$existingUser = User::getByOpenId($request['openid']);
				} catch (Exception $ex) {
					Log::error($ex);
				}
				if (!$existingUser) {
					$user->wx_openid = $request['openid'];
					$user->save();
				}
			}
		}

		// 看看请求中是否有传unionid，如果有的话就和现在的账号绑定
		if (isset($request['union_id'])) {
			// 如果此用户没有绑定的unionid而且此unionid没有被别人使用，则可以绑定
			if (!$user->union_id) {
				$existingUser = null;
				try {
					$existingUser = User::getByUnionId($request['union_id']);
				} catch (Exception $ex) {
					Log::error($ex);
				}
				if (!$existingUser) {
					$user->union_id = $request['union_id'];
					$user->save();
				}
			}
		}

		//律师没有权限创建咨询
		if ($user->status == User::STATUS_VERIFIED_LAWYER) {
			return true;
		}
		//获取post_id
		$post_id = isset($request['post_id']) ? $request['post_id'] : null;
		$postService = App::make(postService::class);
		$postService->update_creator_id($user->id, $post_id);
	} catch (\Exception $e) {
		Log::error($e);
	}
});
/*
 * 发布免费咨询
 */
Event::listen('create_free_question', function ($new_post, $post) {

	UserScoresHistory::post();

	// 给相关的律师提示
	$site_lawyer_conf = Config::get('site.notify_lawyers');
	if (true || isset($site_lawyer_conf[$new_post->post_category . '']) && !Config::get('app.debug')) {
		// $lawyer_uids = $site_lawyer_conf[$new_post->post_category . ''];			
		$vals			= DB::table('index_send')->select('user_id')->get();    	
    	$lawyer_uids 	= [];
    	foreach($vals as $val) {
    	 	 $lawyer_uids[]	=	$val->user_id;
    	}        
    	    	
		$snsmembers = User::whereIn('id', $lawyer_uids)->get();
		// 发送微信消息给律师
		foreach ($snsmembers as $snsmember) {
			if ($snsmember->wx_openid) {
				MsgHelper::sendQuestionToLawyer($snsmember->wx_openid, $new_post->type, $new_post->id, $new_post->title, $new_post->creatorName(), $new_post->content);
				Log::info('send to lawyer: ' . $snsmember->wx_openid . " - snsmember id: " . $snsmember->id . " - post creator: " . $new_post->creatorName() . " - " . $new_post->content . " - " . $new_post->id);
				/**
				 * 微信推送
				 */
				//MsgHelper::sendQuestionToLawyer($snsmember->wx_openid, '', $new_post->id, $new_post->title, $new_post->creator, $new_post->content);
			}
		}
	}

	$user_ids = Lawyer::getUserByScope($new_post->post_category);
	if (!empty($user_ids)) {
		//推送极光消息
		$alias = array_pluck($user_ids->toArray(), 'user_id');
		$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
		$platform = 'all';
		$audience = ['alias' => $alias];
		$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_CREATE_FREE_POST;
		$extras = [
			'notification_type' => $notification_type,
			'post_id'           => $new_post->id,
			'article_id'        => '',
			'post_type'         => '',
			'user_type'         => '',
			'target_id'         => '',
			'target_name'       => '',
			'user_id'           => '',

		];
		$message = str_limit($new_post->title, 10, '.....');
		$JpushService->push($platform, $audience, $message, $extras);
	}

});

//关注
Event::listen('lawyer_follow', function ($user) {

});

//收藏文章
Event::listen('add_collect', function () {
	if (!empty($user)) {
		//UserScoresHistory::collect($user);
	}
});

//点赞
Event::listen('like', function ($user, $currentLikesCount) {
	if ($user) {
		UserScoresHistory::like($user);
	}
});

//钱包提现操作
Event::listen('waller_money_out_user_bankcard', function ($user_id) {

});

//打赏钱
Event::listen('reward', function ($user_id) {
	//推送极光消息
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $user_id];
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_REWARD;
	$extras = [
		'notification_type' => $notification_type,
		'user_id'           => $user_id,
		'article_id'        => '',
		'post_id'           => '',
		'post_type'         => '',
		'user_type'         => '',
		'target_id'         => '',
		'target_name'       => '',

	];
	$message = '收到用户打赏,点击查看';
	$JpushService->push($platform, $audience, $message, $extras);
});
//付费咨询支付订单(钱包支付+ping++)
Event::listen('pay_post', function ($question) {
	$user = PostThread::getUserIdByPostId($question->id);
	$user = User::find($user->id);
	$alias = $user->user_id;

	//推送极光消息
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $alias];
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_PAY_POST_PAY;
	$extras = [
		'notification_type' => $notification_type,
		'post_id'           => $question->id,
		'post_type'         => $question->type,
		'user_type'         => ($user->status == User::STATUS_VERIFIED_LAWYER ? 'lawyer' : 'user'),
		'user_id'           => $user->id,
		'target_id'         => '',
		'target_name'       => '',
		'article_id'        => '',

	];
	$message = '你收到用户的一对一咨询,点击受理订单';
	$JpushService->push($platform, $audience, $message, $extras);
});

//付费咨询及时通讯(一对一咨询)
Event::listen('pay_post_reply', function ($comment, $question) {
	UserScoresHistory::comment();

	$user = PostThread::getUserIdByPostId($question->post_id);
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_PAY_POST_REPLY_HUANXIN;
	if ($question->creator_id == $comment->creator_id) {
		$alias = $user->user_id;
		$target_id = $question->creator_id;
		$target_name = empty($user->nikname) ? $user->realname : $user->nikname;
	} else {
		$alias = $question->creator_id;
		$target_id = $alias = $user->user_id;
		$target_name = empty($user->nikname) ? $user->realname : $user->nikname;
	}

	//推送极光消息
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $alias];
	$extras = [
		'notification_type' => $notification_type,
		'target_id'         => $target_id,
		'target_name'       => $target_name,
		'post_id'           => '',
		'post_type'         => '',
		'user_type'         => '',
		'user_id'           => '',
		'article_id'        => '',


	];
	$message = str_limit($comment->content, 10, '.....');
	$JpushService->push($platform, $audience, $message, $extras);
});
//免费咨询回复
Event::listen('free_post_reply', function ($comment, $question) {
	UserScoresHistory::comment();

	$alias = $question->creator_id;
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_FREE_POST_REPLY;

	//推送极光消息
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $alias];
	$extras = [
		'notification_type' => $notification_type,
		'post_id'           => $question->id,
		'article_id'        => '',
		'post_type'         => '',
		'user_type'         => '',
		'target_id'         => '',
		'target_name'       => '',
		'user_id'           => '',

	];
	$message = str_limit($comment->content, 10, '.....');
	$JpushService->push($platform, $audience, $message, $extras);
});
//律师确认受理咨询
Event::listen('lawyer_accept', function ($post) {
	//推送极光消息
	$alias = $post->creator_id;
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $alias];
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_LAWYER_POST_CONFIRM;
	$extras = [
		'notification_type' => $notification_type,
		'post_id'           => $post->id,
		'post_type'         => $post->type,
		'user_type'         => 'user',
		'target_id'         => '',
		'target_name'       => '',
		'user_id'           => '',
		'article_id'        => '',

	];
	$message = '你的订单律师已经受理,点击联系律师';
	$JpushService->push($platform, $audience, $message, $extras);
});
//律师文章被评论
Event::listen('comment_by_lawyer_for_article', function ($lawyer_user_id, $comment_content, $article) {
	//推送极光消息
	$alias = $lawyer_user_id;
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $alias];
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_LAWYER_ARTICLE_COMMENT;
	$extras = [
		'notification_type' => $notification_type,
		'article_id'        => $article->id,
		'post_id'           => '',
		'post_type'         => '',
		'user_type'         => '',
		'target_id'         => '',
		'target_name'       => '',
		'user_id'           => '',

	];
	$message = str_limit($comment_content, 10, '.....');
	$JpushService->push($platform, $audience, $message, $extras);
});
//律师认证审核成功
Event::listen('verify_lawyer', function ($user) {
	//推送极光消息
	$alias = $user->id;
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $alias];
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_LAWYER_SUCCESS_VERIFY;
	$extras = [
		'notification_type' => $notification_type,
		'user_type'         => 2,
		'article_id'        => '',
		'post_id'           => '',
		'user_type'         => '',
		'target_id'         => '',
		'target_name'       => '',
		'user_id'           => '',

	];
	$message = '你好,认证律师审核通过';
	$JpushService->push($platform, $audience, $message, $extras);
});
//成为律师
Event::listen('become_lawyer', function ($user) {
	UserScoresHistory::comment();
});

//发表文章
Event::listen('create_article', function ($article) {

});

//收到咨询评价
Event::listen('evaluate', function ($post) {

	$PostThread = PostThread::getUserIdByPostId($post->id);
	//推送极光消息
	$alias = $PostThread->user_id;
	$JpushService = \Illuminate\Support\Facades\App::make(\App\Services\JpushService::class);
	$platform = 'all';
	$audience = ['alias' => $alias];
	$notification_type = \App\Services\JpushService::NOTIFICATION_TYPE_GET_POST_COMMENT;
	$extras = [
		'notification_type' => $notification_type,
		'post_id'           => $post->id,
		'article_id'        => '',
		'post_type'         => '',
		'user_type'         => '',
		'target_id'         => '',
		'target_name'       => '',
		'user_id'           => '',

	];
	$message = '收到用户的评价,点击查看';
	$JpushService->push($platform, $audience, $message, $extras);
});


